package com.intel.factorytest.ChamberMode;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Looper;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.app.ActionBar;
import android.os.SystemProperties;

import com.intel.factorytest.R;
import com.intel.factorytest.util.LogUtils;
import com.intel.factorytest.util.Util;
import com.intel.factorytest.util.ShellUtilsPlus;
import com.intel.factorytest.ChamberMode.ChamberModeMainActivity;
import com.intel.factorytest.ChamberMode.ChambermodeBaseActivity;




public class LTEBandTest extends ChambermodeBaseActivity {
    
    private final static int LTE_BAND_TEST_MSG_UPDATE_UI = 1;
    private final static int LTE_BAND_TEST_MSG_STOP_TEST = 2;

    private final static int LTE_BAND_TEST_MSG_BAND2 = 3;
    private final static int LTE_BAND_TEST_MSG_BAND5 = 4;
    private final static int LTE_BAND_TEST_MSG_BAND7 = 5;
    private final static int LTE_BAND_TEST_MSG_BAND21 = 6;
    private final static int LTE_BAND_TEST_MSG_BAND48 = 7;    
    private final static int LTE_BAND_TEST_MSG_FTM_IN = 8;    
    private final static int LTE_BAND_TEST_MSG_FTM_OUT = 9;
    private final static int LTE_BAND_TEST_MSG_BAND_STOP = 10;
    private final static int LTE_BAND_TEST_MSG_END = 11;
    private final static int LTE_BAND_TEST_MSG_REBOOT = 12;
    
    private static String LTE_TRAN_IN_DIAG_CMD_FILE = "/system/bin/diagcmd/bandtestin.txt";    
    private static String LTE_TRAN_OUT_DIAG_CMD_FILE = "/system/bin/diagcmd/bandtestout.txt";    
    private static String LTE_BAND_STOP_DIAG_CMD_FILE = "/system/bin/diagcmd/bandstop.txt";
    private static String LTE_BAND2_DIAG_CMD_FILE = "/system/bin/diagcmd/Vail_LTE_band2.txt";
    private static String LTE_BAND5_DIAG_CMD_FILE = "/system/bin/diagcmd/Vail_LTE_band5.txt";
    private static String LTE_BAND7_DIAG_CMD_FILE = "/system/bin/diagcmd/Vail_LTE_band7.txt";
    private static String LTE_BAND21_DIAG_CMD_FILE = "/system/bin/diagcmd/Vail_LTE_band21.txt";
    private static String LTE_BAND48_DIAG_CMD_FILE = "/system/bin/diagcmd/Vail_LTE_band48.txt";

    Context mContext;
    ShellUtilsPlus.CommandResult mCmdResult;
    private String cmdSuccessMsg;
    private String cmdErrorMsg;
    private int LTE_BAND_TEST_MSG = LTE_BAND_TEST_MSG_BAND2;
    private boolean isTesting = false;

    
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {            
            LogUtils.logd("mHandler msg.what = " + msg.what);
            switch (msg.what) {
                case LTE_BAND_TEST_MSG_UPDATE_UI: {
                    removeMessages(LTE_BAND_TEST_MSG_UPDATE_UI);
                    updateTitle("");
                    break;
                }
                case LTE_BAND_TEST_MSG_STOP_TEST: {
                    removeMessages(LTE_BAND_TEST_MSG_STOP_TEST);
                    finish();
                    break;
                }
                case LTE_BAND_TEST_MSG_FTM_IN: {
                    removeMessages(LTE_BAND_TEST_MSG_FTM_IN);
                    enterFtmMode();
                    sendDelayedMsg(LTE_BAND_TEST_MSG_BAND2,2000,null,null,mHandler);                    
                    break;
                }
                case LTE_BAND_TEST_MSG_BAND2: {
                    removeMessages(LTE_BAND_TEST_MSG_BAND2);
                    updateTitle("LTE Band 2");
                    SystemProperties.set("persist.sys.chamber.bandtest","lteband02");
                    //startTestThread(LTE_BAND2_DIAG_CMD_FILE);
                    sendDelayedMsg(LTE_BAND_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
                    sendDelayedMsg(LTE_BAND_TEST_MSG_BAND_STOP,20000,null,null,mHandler);
                    break;
                }
                case LTE_BAND_TEST_MSG_BAND5: {
                    removeMessages(LTE_BAND_TEST_MSG_BAND5);                    
                    updateTitle("LTE Band 5");                    
                    //startTestThread(LTE_BAND5_DIAG_CMD_FILE);
                    SystemProperties.set("persist.sys.chamber.bandtest","lteband05");
                    sendDelayedMsg(LTE_BAND_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
                    sendDelayedMsg(LTE_BAND_TEST_MSG_BAND_STOP,20000,null,null,mHandler);
                    break;
                }
                case LTE_BAND_TEST_MSG_BAND7: {
                    removeMessages(LTE_BAND_TEST_MSG_BAND7);
                    updateTitle("LTE Band 7");
                    //startTestThread(LTE_BAND7_DIAG_CMD_FILE);                    
                    SystemProperties.set("persist.sys.chamber.bandtest","lteband07");
                    sendDelayedMsg(LTE_BAND_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
                    sendDelayedMsg(LTE_BAND_TEST_MSG_BAND_STOP,20000,null,null,mHandler);
                    break;
                }
                case LTE_BAND_TEST_MSG_BAND21: {
                    removeMessages(LTE_BAND_TEST_MSG_BAND21);
                    updateTitle("LTE Band 21");
                    //startTestThread(LTE_BAND21_DIAG_CMD_FILE);                    
                    SystemProperties.set("persist.sys.chamber.bandtest","lteband21");
                    sendDelayedMsg(LTE_BAND_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
                    sendDelayedMsg(LTE_BAND_TEST_MSG_BAND_STOP,20000,null,null,mHandler);
                    break;
                }
                case LTE_BAND_TEST_MSG_BAND48: {
                    removeMessages(LTE_BAND_TEST_MSG_BAND48);
                    updateTitle("LTE Band 48");
                    //startTestThread(LTE_BAND48_DIAG_CMD_FILE);                    
                    SystemProperties.set("persist.sys.chamber.bandtest","lteband48");
                    sendDelayedMsg(LTE_BAND_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
                    sendDelayedMsg(LTE_BAND_TEST_MSG_BAND_STOP,20000,null,null,mHandler);
                    break;
                }
                case LTE_BAND_TEST_MSG_BAND_STOP: {
                    removeMessages(LTE_BAND_TEST_MSG_BAND_STOP);
                    //startTestThread(LTE_BAND_STOP_DIAG_CMD_FILE);
                    SystemProperties.set("persist.sys.chamber.bandtest","ltebandstop");
                    LTE_BAND_TEST_MSG ++;
                    LogUtils.logd("mHandler LTE_BAND_TEST_MSG_BAND_STOP LTE_BAND_TEST_MSG = " + LTE_BAND_TEST_MSG);
                    if ((LTE_BAND_TEST_MSG >= LTE_BAND_TEST_MSG_BAND2) && (LTE_BAND_TEST_MSG <= LTE_BAND_TEST_MSG_BAND48)) {
                        sendDelayedMsg(LTE_BAND_TEST_MSG,6000,null,null,mHandler);
                    } else if (LTE_BAND_TEST_MSG == (LTE_BAND_TEST_MSG_BAND48 + 1)) {
                        sendDelayedMsg(LTE_BAND_TEST_MSG_END,2000,null,null,mHandler);
                    } else {
                        LTE_BAND_TEST_MSG = LTE_BAND_TEST_MSG_BAND2;
                        LogUtils.loge("mHandler LTE_BAND_TEST_MSG_BAND_STOP do nothing");
                    }
                    break;
                }
                case LTE_BAND_TEST_MSG_FTM_OUT: {
                    removeMessages(LTE_BAND_TEST_MSG_FTM_OUT);
                    Util.isLTEBandTesting = false ;                    
                    exitFtmMode();
                    finish();
                    break;
                }
                case LTE_BAND_TEST_MSG_END: {
                    removeMessages(LTE_BAND_TEST_MSG_END);
                    Util.isLTEBandTesting = false ;
                    finish();
                    break;
                }
                case LTE_BAND_TEST_MSG_REBOOT: {
                    removeMessages(LTE_BAND_TEST_MSG_REBOOT);
                    //ChamberModeMainActivity.rebootToQMMI(mContext);
                    break;
                }

                default:
                    break;
            }
        }
    };

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);        
        LogUtils.loge("onCreate");
        mContext = getApplicationContext();        
    }

    @Override
    public void onResume() {
        LogUtils.loge("onResume");        
        SystemProperties.set("persist.sys.chamber_run","false");        
        super.onResume();        
    }
    
    @Override
    protected void startTest(){
        LogUtils.logi("startLTEBandTest");
        Util.isLTEBandTesting = true ;
        updateTitle("LTE Band");
        sendDelayedMsg(LTE_BAND_TEST_MSG_UPDATE_UI,5000,null,null,mHandler);
        sendDelayedMsg(LTE_BAND_TEST_MSG_FTM_IN,1000,null,null,mHandler);
    }
    
    @Override
    protected void stopTest(){
        LogUtils.logi("stopLTEBandTest");
        startTestThread(LTE_BAND_STOP_DIAG_CMD_FILE);
        sendDelayedMsg(LTE_BAND_TEST_MSG_FTM_OUT,1000,null,null,mHandler);
    }

    private Thread mLteBandTransmitThread;

    public void startTestThread(String path) {        
        LogUtils.logi( "startTestThread path = " + path);
        mLteBandTransmitThread = new Thread(new LteBandTransmitRunnable(path));
        mLteBandTransmitThread.start();
    }


    private void enterFtmMode() {
        LogUtils.logi("enterFtmMode");
        updateMessage("FTM Mode");
        //startTestThread(LTE_TRAN_IN_DIAG_CMD_FILE);
        SystemProperties.set("persist.sys.chamber.bandtest","ftmin");
    }
    
    private void exitFtmMode() {
        LogUtils.logi("exitFtmMode");        
        SystemProperties.set("persist.sys.chamber.bandtest","ftmout");
        //startTestThread(LTE_TRAN_OUT_DIAG_CMD_FILE);
    }

    @Override
    public void onPause() {
        super.onPause();
        LogUtils.loge( "onPause() ");
    }

    @Override
    public void onStop() {
        super.onPause();
        LogUtils.loge( "onStop() ");
        //exitFtmMode();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtils.loge( "onDestroy() ");        
        SystemProperties.set("persist.sys.chamber.bandtest","null");
    }
    
    @Override
    public void finish() {    
        SystemProperties.set("persist.sys.chamber_run","true");
        Intent i = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString(ChamberModeMainActivity.LTE_BAND_RESULT, "lte_band_test_finish");        
        i.putExtras(bundle);
        setResult(ChamberModeMainActivity.START_LTE_BAND_TEST_RESULT_CODE,i);
        super.finish();
    }
    
    private class LteBandTransmitRunnable implements Runnable{

        String bandPath;
        public LteBandTransmitRunnable(String bandPath){
            this.bandPath = bandPath;
        }

        @Override
        public void run() {
            LogUtils.logi("LteBandTransmitRunnable run() bandpath = " + bandPath);
            mCmdResult = ShellUtilsPlus.execCommand("diag_tools -f" + bandPath,false);
            cmdSuccessMsg = mCmdResult.successMsg;
            cmdErrorMsg = mCmdResult.errorMsg;
            LogUtils.logi( "LteBandTransmitRunnable get Success Msg = " + cmdSuccessMsg + ", Error Msg = " + cmdErrorMsg);
        }
    }

/*
    private class countDownThread extends Thread{
        public countDownThread(){}

        @Override
        public void run() {
            LogUtils.logi("countDownThread  run()");
            // TODO Auto-generated method stub
            Looper.prepare();
            mBandTestDown = new BandTestDown();
            mBandTestDown.start();
            Looper.loop();
        }
    }

    private class BandTestDown extends CountDownTimer{

        public BandTestDown() {
            super(mTimeout,mTimeout);
        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            LogUtils.logi("Time Out:");
            mHandler.sendEmptyMessage(LTE_BAND_TEST_MSG_TIME_OUT);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            // TODO Auto-generated method stub
        }
    }
*/    

}

